package org.ade.monak.anak.service.gate.monak;

import org.ade.monak.anak.Variable.Status;
import org.ade.monak.anak.Variable.TipeKoneksi;
import org.ade.monak.anak.Variable.TipePesanMonak;
import org.ade.monak.anak.entity.Anak;
import org.ade.monak.anak.entity.Lokasi;
import org.ade.monak.anak.entity.Ortu;
import org.ade.monak.anak.map.service.GpsManager;
import org.ade.monak.anak.service.gate.SenderInternet;
import org.ade.monak.anak.service.gate.SenderSMS;
import org.ade.monak.anak.service.storage.LogMonakFileManager;
import org.ade.monak.anak.service.storage.PreferenceMonitoringManager;
import org.ade.monak.anak.util.EntityBundleMaker;
import org.ade.monak.anak.util.IDGeneratorAnak;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class SenderLokasi{
	
	public SenderLokasi(Context context, Ortu ortu) {
		this.ortu = ortu;
		this.context = context;
		this.senderSMS = new SenderSMS(context, null);
		this.senderInternet = new SenderInternet(context, new HandlerSenderInternetMonitoring(this));
	}
	
	public void sendLocationSingleRequest(String id){
		
		PreferenceMonitoringManager pref = new PreferenceMonitoringManager(context);
		Lokasi lokasi = pref.getLastLokasi();
		
		if(lokasi!=null && lokasi.getlatitude()!=0){
			sendLocationSingleRequest(lokasi, id);
		}else{
			GpsManager gpsManager = new GpsManager(context, new LocationReceiverHandler(this, id));
	    	lokasi = gpsManager.getLastLokasi();
	    	if(lokasi!=null){
	    		sendLocationSingleRequest(lokasi, id);	
	    	}else{
	    		gpsManager.searchLokasi();
	    	}	
		}
		
		
	}

	public void sendLocationSingleRequest(Lokasi lokasi,  String idAnak){
		Anak anak = new Anak();
		anak.setIdAnak(idAnak);
		anak.setLastLokasi(lokasi);
		sendLocationSingleRequest(anak);
	}
	
	public void sendLocationModeTracking(Anak anak){
		send(anak, TipePesanMonak.RETRIEVE_TRACKING);
	}
	
	public void sendLocationSingleRequest(Anak anak){
		send(anak,TipePesanMonak.RETRIEVE_LOCATION_ANAK);
	}
	
	private void send(Anak anak, int tipe){
		IDGeneratorAnak idGenerator = new IDGeneratorAnak(context, null);
		
		Lokasi lokasi = anak.getLastLokasi();
		pesan = tipe+","+lokasi.getlatitude()+","+lokasi.getLongitude()+","+lokasi.getTime()+","+anak.getIdAnak()+","+idGenerator.getImei();
		senderInternet.kirimPesan(ortu.getIdOrtu(), pesan);

		LogMonakFileManager.debug(pesan);
	}
	
	public void success(int tipeKoneksi) {

	}
	
	public void failed(int tipeKoneksi) {	
		if(tipeKoneksi==TipeKoneksi.INTERNET){
			senderSMS.kirimPesan(ortu.getNoHp(), pesan);
		}
	}
	
	private final Ortu ortu;
	private final Context context;
	private final SenderSMS senderSMS;
	private final SenderInternet senderInternet;
	private String pesan;
	
	
	private static class LocationReceiverHandler extends Handler{

		public LocationReceiverHandler(SenderLokasi senderLokasi, String idAnak){
			this.senderLokasi 	= senderLokasi;
			this.idAnak			= idAnak;
		}
		
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
				case Status.SUCCESS:{
					Bundle bundle = msg.getData();
					if(bundle!=null){
						Lokasi lokasi = EntityBundleMaker.getLokasiFromBundle(bundle);
						Anak anak = new Anak();
						anak.setIdAnak(idAnak);
						anak.setLastLokasi(lokasi);
						senderLokasi.sendLocationSingleRequest(anak);
					}
					break;
				}case Status.FAILED:{
					break;
				}
			}
			
		}
		private final SenderLokasi 	senderLokasi;
		private final String 		idAnak;
	}
	
	private static final class HandlerSenderInternetMonitoring extends Handler{

		public HandlerSenderInternetMonitoring(SenderLokasi senderMonitoring){
			this.senderMonitoring = senderMonitoring;
		}
		
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
				case Status.SUCCESS:{
					senderMonitoring.success(TipeKoneksi.INTERNET);
					break;
				}case Status.FAILED:{
					senderMonitoring.failed(TipeKoneksi.INTERNET);
					break;
				}
			}
		}
		private SenderLokasi senderMonitoring;
		
	}
	
	
}
